var mapData = createMap();

function expandRight(solidPositions, topLeft, constX, maxY) {
    for (var dy = 0; dy <= maxY; dy++) {
        if (!solidPositions.some(function(pos) {
            return (pos.x === topLeft.x + constX && pos.y === topLeft.y + dy);
        }
        )) {
            return false;
        }
    }
    return true;
}

function expandBottom(solidPositions, topLeft, constY, maxX) {
    for (var dx = 0; dx <= maxX; dx++) {
        if (!solidPositions.some(function(pos) {
            return (pos.x === topLeft.x + dx && pos.y === topLeft.y + constY);
        }
        )) {
            return false;
        }
    }
    return true;
}

function expandQuadratic(solidPositions, topLeft, edgeLength) {
    return expandRight(solidPositions, topLeft, edgeLength, edgeLength) && expandBottom(solidPositions, topLeft, edgeLength, edgeLength)
}

function createMap() {
    var data = jsonData();
    var mapData = new Object();
    mapData.image = new Image();
    mapData.image.src = data.tilesets[0].image;
    mapData.tileSize = data.tilesets[0].tileheight;
    mapData.height = data.height;
    mapData.width = data.width;
    mapData.length = data.layers[0].data.length;
    mapData.tileArray = new Array();
    var solidPositions = new Array();
    mapData.solidAreas = new Array();
    var solidTiles = [8];

    var TILES_PER_ROW = data.tilesets[0].imagewidth / data.tilesets[0].tilewidth;

    for (var i = 0; i < mapData.height; i++) {
        mapData.tileArray[i] = new Array();
        for (var j = 0; j < mapData.width; j++) {
            var index = i * mapData.width + j;

            if (solidTiles.indexOf(data.layers[0].data[index]) !== -1) {
                var position = new Object();
                position.x = j;
                position.y = i;
                solidPositions.push(position);
            }

            tileIndex = data.layers[0].data[index] - 1;
            mapData.tileArray[i][j] = new Object();
            mapData.tileArray[i][j].imageX = tileIndex % TILES_PER_ROW * mapData.tileSize;
            mapData.tileArray[i][j].imageY = Math.floor(tileIndex / TILES_PER_ROW) * mapData.tileSize;
            mapData.tileArray[i][j].worldX = j * mapData.tileSize;
            mapData.tileArray[i][j].worldY = i * mapData.tileSize;
        }
    }

    while (solidPositions.length > 0) {
        var topLeft = solidPositions[0];
        var offset = 0;
        while (expandQuadratic(solidPositions, topLeft, offset + 1)) {
            offset++;
        }
        var x = offset;
        var y = offset;
        while (expandRight(solidPositions, topLeft, x + 1, offset)) {
            x++;
        }
        if (x == offset) {
            while (expandBottom(solidPositions, topLeft, y + 1, offset)) {
                y++;
            }
        }

        var bottomRight = new Object();
        bottomRight.x = topLeft.x + x;
        bottomRight.y = topLeft.y + y;
        var area = new Object();
        area.topLeft = topLeft;
        area.bottomRight = bottomRight;
        mapData.solidAreas.push(area);
        solidPositions = solidPositions.filter((function(pos) {
            return !(pos.x >= topLeft.x && pos.x <= bottomRight.x && pos.y >= topLeft.y && pos.y <= bottomRight.y);
        }));
    }

    return mapData;
}

function drawMap(context, mapData) {

    var defaultAlpha = context.globalAlpha;
    for (var i = 0; i < mapData.height; i++) {
        var row = mapData.tileArray[i];
        for (var j = 0; j < mapData.width; j++) {
            context.drawImage(
                    mapData.image,
                    row[j].imageX, row[j].imageY, mapData.tileSize, mapData.tileSize,
                    row[j].worldX, row[j].worldY, mapData.tileSize, mapData.tileSize
                    );
        }
    }
    context.globalAlpha = defaultAlpha;
}

function getTileCoords(pos) {
    return new Vector(Math.floor(pos.x / data.tilesets[0].tilewidth), Math.floor(pos.y / data.tilesets[0].tileheight));
}

function getTileCount() {
    return mapData.height;
}

function getTileSize() {
    return mapData.tileSize;
}

function jsonData() {
    var data = {"height": 50,
        "layers": [
            {
                "data": [27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
                    27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
                    27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 8, 8, 8, 8, 8, 8, 27, 27,
                    27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 8, 27, 27, 27, 27, 8, 27, 27,
                    27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 8, 27, 27, 27, 27, 8, 27, 27,
                    27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 8, 27, 27, 27, 27, 8, 27, 27,
                    27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 8, 8, 8, 8, 8, 8, 8, 27, 27, 27, 27, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 27, 27, 8, 27, 27, 8, 27, 27, 27, 27, 8, 27, 27,
                    27, 27, 27, 8, 27, 27, 8, 8, 8, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 8, 27, 27, 8, 27, 27, 27, 27, 8, 27, 27,
                    27, 27, 27, 8, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 8, 27, 27, 8, 8, 8, 8, 8, 8, 27, 27,
                    27, 27, 27, 8, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 8, 8, 27, 27, 27, 8, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
                    27, 27, 27, 8, 27, 27, 8, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 8, 27, 27, 27, 8, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
                    27, 27, 27, 8, 27, 27, 8, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 8, 27, 27, 27, 8, 27, 27, 8, 27, 27, 8, 8, 8, 8, 8, 8, 8, 8,
                    8, 8, 8, 8, 27, 27, 8, 27, 27, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 27, 27, 8, 27, 27, 27, 8, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
                    27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 8, 8, 27, 27, 8, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
                    27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 8, 8, 8, 8, 8, 8, 8, 8, 8, 27, 27,
                    27, 27, 27, 27, 27, 27, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 8, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 8, 138, 138, 8, 138, 138, 138, 138, 138, 138, 27, 8, 27, 27,
                    27, 27, 27, 27, 27, 27, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 8, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 8, 138, 138, 8, 138, 138, 138, 138, 138, 138, 27, 8, 27, 27,
                    27, 27, 27, 27, 27, 27, 138, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 138, 138, 138, 138, 138, 8, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 8, 138, 138, 8, 138, 138, 138, 8, 138, 138, 27, 8, 27, 27,
                    27, 27, 27, 27, 27, 27, 138, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 138, 138, 138, 138, 138, 8, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 8, 138, 138, 8, 138, 138, 138, 8, 138, 138, 27, 8, 27, 27,
                    27, 27, 27, 27, 27, 27, 138, 138, 138, 138, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 138, 138, 8, 138, 82, 82, 82, 82, 82, 82, 82, 82, 82, 8, 82, 82, 8, 82, 82, 138, 8, 138, 138, 27, 8, 27, 27,
                    27, 27, 27, 27, 27, 27, 138, 138, 138, 138, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 138, 138, 8, 138, 82, 82, 82, 82, 82, 82, 82, 82, 82, 8, 82, 82, 8, 82, 82, 138, 8, 138, 138, 27, 8, 27, 27,
                    8, 8, 8, 8, 8, 8, 8, 8, 138, 138, 82, 82, 82, 82, 82, 8, 8, 8, 8, 8, 8, 8, 8, 138, 138, 138, 138, 82, 82, 82, 82, 82, 82, 82, 82, 82, 8, 82, 82, 8, 82, 82, 138, 8, 138, 138, 27, 8, 27, 27,
                    27, 27, 27, 27, 27, 27, 138, 138, 138, 138, 82, 82, 82, 82, 82, 8, 82, 82, 82, 82, 82, 82, 8, 138, 138, 138, 138, 82, 82, 82, 82, 82, 82, 82, 82, 82, 8, 82, 82, 8, 82, 82, 138, 8, 138, 138, 27, 8, 27, 27,
                    27, 27, 27, 27, 27, 27, 138, 138, 138, 138, 82, 82, 82, 82, 82, 8, 82, 82, 82, 82, 82, 82, 8, 138, 138, 8, 8, 8, 8, 8, 8, 8, 8, 82, 82, 82, 8, 82, 82, 8, 82, 82, 138, 8, 138, 138, 27, 8, 27, 27,
                    27, 27, 27, 27, 8, 8, 8, 8, 8, 8, 8, 8, 8, 82, 82, 8, 82, 82, 82, 82, 82, 82, 8, 138, 138, 138, 138, 82, 82, 82, 82, 82, 8, 82, 82, 82, 8, 82, 82, 8, 82, 82, 138, 8, 138, 138, 27, 8, 27, 27,
                    8, 8, 27, 27, 8, 27, 138, 138, 138, 138, 82, 82, 82, 82, 82, 8, 82, 82, 82, 82, 82, 82, 8, 138, 138, 138, 138, 82, 82, 82, 82, 82, 8, 82, 82, 82, 8, 82, 82, 8, 82, 82, 138, 8, 138, 138, 27, 8, 27, 27,
                    8, 8, 27, 27, 8, 27, 138, 138, 138, 138, 82, 82, 82, 82, 82, 8, 82, 82, 82, 82, 82, 82, 8, 138, 138, 138, 138, 82, 82, 82, 82, 82, 8, 82, 82, 82, 8, 82, 82, 8, 82, 82, 138, 8, 138, 138, 27, 8, 27, 27,
                    27, 27, 27, 27, 8, 27, 138, 138, 138, 138, 82, 82, 82, 82, 82, 8, 82, 82, 82, 82, 82, 82, 8, 138, 138, 138, 138, 82, 82, 82, 82, 82, 8, 82, 82, 82, 8, 82, 82, 8, 82, 82, 138, 8, 138, 138, 27, 8, 27, 27,
                    27, 27, 27, 27, 8, 27, 138, 138, 138, 138, 82, 82, 82, 82, 82, 8, 82, 82, 82, 82, 82, 82, 8, 138, 138, 138, 138, 82, 82, 82, 82, 82, 8, 82, 82, 82, 8, 82, 82, 8, 82, 82, 138, 8, 138, 138, 27, 27, 27, 27,
                    27, 27, 27, 27, 8, 27, 138, 138, 138, 138, 82, 82, 82, 82, 82, 8, 8, 8, 8, 8, 8, 8, 8, 138, 138, 138, 138, 82, 82, 82, 82, 82, 8, 82, 82, 82, 8, 82, 82, 8, 8, 8, 8, 8, 138, 138, 27, 27, 27, 27,
                    27, 27, 8, 8, 8, 27, 138, 138, 138, 138, 82, 82, 82, 82, 82, 8, 82, 82, 82, 82, 82, 82, 82, 138, 138, 138, 138, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 138, 138, 138, 138, 27, 8, 27, 27,
                    27, 27, 8, 8, 8, 27, 138, 8, 8, 8, 8, 8, 8, 82, 82, 8, 82, 82, 82, 82, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 138, 138, 138, 138, 27, 8, 27, 27,
                    27, 27, 27, 27, 8, 138, 138, 8, 8, 8, 8, 8, 8, 82, 82, 8, 82, 82, 82, 82, 138, 138, 138, 138, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 138, 138, 27, 8, 27, 27,
                    27, 27, 27, 27, 8, 138, 138, 138, 138, 138, 138, 138, 82, 82, 82, 82, 82, 82, 82, 82, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 82, 138, 138, 138, 138, 27, 8, 27, 27,
                    27, 27, 27, 27, 8, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 27, 8, 27, 27,
                    27, 27, 27, 27, 8, 138, 138, 138, 138, 138, 138, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 138, 138, 8, 8, 8, 8, 8, 8, 8, 8, 8, 138, 138, 8, 138, 138, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 27, 27,
                    8, 8, 27, 27, 8, 138, 138, 138, 138, 138, 138, 8, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 8, 138, 138, 8, 138, 138, 138, 138, 138, 138, 138, 27, 27, 27, 27,
                    8, 8, 27, 27, 8, 138, 138, 138, 138, 138, 138, 8, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 8, 138, 138, 8, 138, 138, 138, 138, 138, 138, 138, 27, 27, 27, 27,
                    27, 27, 27, 27, 8, 138, 138, 138, 138, 138, 138, 8, 27, 27, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 138, 138, 138, 8, 8, 8, 8, 82, 82, 8, 82, 82, 8, 82, 82, 8, 8, 8, 8, 8, 8, 8, 27, 27,
                    27, 27, 27, 27, 8, 138, 138, 138, 138, 138, 138, 8, 27, 27, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 138, 138, 138, 8, 8, 8, 8, 82, 82, 8, 82, 82, 8, 82, 82, 8, 8, 8, 8, 8, 8, 8, 27, 27,
                    27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 82, 82, 82, 82, 82, 8, 82, 82, 82, 82, 82, 82, 138, 138, 138, 138, 27, 27, 27, 27,
                    27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 138, 138, 138, 138, 82, 82, 82, 82, 82, 82, 82, 82, 8, 82, 82, 82, 82, 82, 82, 138, 138, 138, 138, 27, 27, 27, 27,
                    27, 27, 27, 27, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 138, 138, 138, 8, 8, 8, 8, 8, 8, 8, 8, 8, 82, 82, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
                    27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 138, 138, 138, 138, 82, 82, 82, 82, 82, 82, 82, 82, 8, 82, 82, 82, 82, 82, 82, 138, 138, 138, 138, 27, 27, 27, 27,
                    27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 138, 138, 138, 138, 82, 82, 82, 82, 82, 82, 82, 82, 8, 82, 82, 82, 82, 82, 82, 138, 138, 138, 138, 27, 27, 27, 27,
                    27, 27, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 27, 27, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 138, 138, 8, 138, 138, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 27, 27,
                    27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 8, 27, 27, 27, 27, 27, 27, 138, 138, 138, 138, 138, 138, 138, 138, 138, 8, 138, 138, 8, 138, 138, 8, 138, 138, 138, 138, 138, 138, 138, 27, 8, 27, 27,
                    27, 27, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 27, 27, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 138, 138, 8, 138, 138, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 27, 27,
                    27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 8, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 27, 27, 27, 27,
                    27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27],
                "height": 50,
                "name": "Kachelebene 1",
                "opacity": 1,
                "type": "tilelayer",
                "visible": true,
                "width": 50,
                "x": 0,
                "y": 0
            }],
        "orientation": "orthogonal",
        "properties":
                {
                },
        "tileheight": 32,
        "tilesets": [
            {
                "firstgid": 1,
                "image": "tileset3.png",
                "imageheight": 544,
                "imagewidth": 544,
                "margin": 0,
                "name": "tileset3",
                "properties":
                        {
                        },
                "spacing": 0,
                "tileheight": 32,
                "tilewidth": 32
            }],
        "tilewidth": 32,
        "version": 1,
        "width": 50
    };
    return data;
}